package com.skyworth.web.admin.user.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.skyworth.web.admin.role.domain.RoleModel;
import com.skyworth.web.admin.role.repository.RoleRepo;
import com.skyworth.web.admin.store.model.StoreModel;
import com.skyworth.web.admin.store.repository.StoreRepository;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.persistence.*;

@Entity
@Component
@Table(name = "ff_user")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserModel {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    public long id;

    @Column(name="employee_no")
    public String employeeNo;

    public String username;

    public String mobile;

    @JsonIgnore
    public String password;

    public String accessToken;

    public UserKind kind;

    @Column(name="store_id")
    public long storeId;

    @Column(name="company_id")
    public long companyId;

    @Column(name="role_id")
    public long roleId;

    @Transient
    private static RoleRepo roleRepo;

    @Autowired
    public void setRoleRepo(RoleRepo roleRepo){
        UserModel.roleRepo = roleRepo;
    }

    @Transient
    private static StoreRepository storeRepository;

    @Autowired
    public void setStoreRepository(StoreRepository storeRepository) {
        UserModel.storeRepository = storeRepository;
    }

    public String getStoreName(){
        StoreModel storeModel = UserModel.storeRepository.findOne(this.storeId);
        if(null != storeModel){
            return storeModel.name;
        }else {
            return "skyworth";
        }
    }
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public UserKind getKind() {
        return kind;
    }

    public String getKindName() {
        return kind.kindName();
    }

    public void setKind(UserKind kind) {
        this.kind = kind;
    }

    @Override
    public String toString() {
        return this.username;
    }

    public long getRoleId() {
        return roleId;
    }

    public void setRoleId(long roleId) {
        this.roleId = roleId;
    }

    public RoleModel getRoleModel(){
        return UserModel.roleRepo.findOne(this.getRoleId());
    }

    public String getEmployeeNo() {
        return employeeNo;
    }

    public void setEmployeeNo(String employeeNo) {
        this.employeeNo = employeeNo;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public long getStoreId() {
        return storeId;
    }

    public void setStoreId(long storeId) {
        this.storeId = storeId;
    }

    public long getCompanyId() {
        return companyId;
    }

    public void setCompanyId(long companyId) {
        this.companyId = companyId;
    }
}
